/**
 *
 */
package com.et114.core.dao.impl;

import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.et114.core.dao.IBaseDAO;
import com.et114.core.dao.vo.BaseVO;
import com.et114.core.exception.CheckException;
import com.et114.core.utility.BeanUtils;
import com.et114.core.utility.DateUtil;

/*
 * @author guanhw
 */
public class BaseDAOImpl implements IBaseDAO {
	protected Log logger = LogFactory.getLog( this.getClass( ) );
	public SqlMapClientTemplate sqlMapClientTemplate = null ;

	/**
	 * 扩展dao 需声明sqlMapTemplate
	 * @param sqlMapTemplate
	 */
	public int delete(	BaseVO entity	, String method	) throws CheckException {
		// TODO Auto-generated method stub
		return this.sqlMapClientTemplate.delete( method  , entity ) ;
	}

	public int delete( Object entity , String method   ) throws CheckException {
		return this.sqlMapClientTemplate.delete ( method , entity ) ;
	}

	public Long getCount( BaseVO entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return Long.valueOf ( sqlMapClientTemplate.queryForObject( method , entity ).toString ( ) ) ;
	}


	public Long getCount( Object entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return ( Long )this.sqlMapClientTemplate.queryForObject( method , entity ) ;
	}


	public String getUserLanguage() {
		// TODO Auto-generated method stub
		return null;
	}

	public Object insert( BaseVO entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		try {
			String crtTime = BeanUtils.getProperty ( entity , "crtTime" ) ;
			if ( crtTime == null ) entity.setCrtTime ( DateUtil.getToday ( ) ) ;
		}
		catch ( Exception e ) {
			//logger.error ( e.getMessage ( ) , e ) ;
		}
		 return this.sqlMapClientTemplate.insert( method , entity  ) ;
	}




	public BaseVO load( BaseVO entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return ( BaseVO ) this.sqlMapClientTemplate.queryForObject( method , entity );
	}

	public Object load( Object entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return  this.sqlMapClientTemplate.queryForObject( method , entity );
	}


	public List queryList( BaseVO entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return  this.sqlMapClientTemplate.queryForList( method , entity );
	}

	public List queryList( Object entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return  this.sqlMapClientTemplate.queryForList( method , entity );
	}

	public int update( BaseVO entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		try {
			String updtTime = BeanUtils.getProperty ( entity , "updtTime" ) ;
			if ( updtTime == null ) entity.setUpdtTime ( DateUtil.getToday ( ) ) ; 
		}
		catch ( Exception e ) {
			//logger.error ( e.getMessage ( ) , e ) ;
		}
		return this.sqlMapClientTemplate.update( method , entity  ) ;
	}

	public int update( Object entity , String method ) throws CheckException {
		// TODO Auto-generated method stub
		return this.sqlMapClientTemplate.update( method , entity  ) ;
	}



	public int updateForIntercurrent( BaseVO entity , String updmethod ,
			String selectmethod ) throws CheckException {
		// TODO Auto-generated method stub
		return 0;
	}


	/**
	 * for oracle
	 * @param sequenceId
	 * @return
	 * @throws CheckException
	 */
	public Long getSequence( String sequenceId ) throws CheckException {
		return ( Long ) sqlMapClientTemplate.queryForObject ( "getSequence" , sequenceId  ) ;
	}



	/**
	 *
	 * @param sqlMapClientTemplate
	 */
	public void setSqlMapClientTemplate( SqlMapClientTemplate sqlMapClientTemplate ) {
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}
	/**
	 *
	 * @param hibernateTemplate
	 */


}
